/**
 * @description 是jx对象拷贝处理封装。
 * 对外暴露出 vhObject 实例
 * 深拷贝：把复制的对象所引用的全部对象都复制一遍
 * 浅拷贝：仅仅复制对象的引用，而不是对象本身
 *
 * @example
 * import vhObject from './Object'
 * this.$jxUtils.vhObject.deepClone({name:'jack'})
 * this.$jxUtils.vhObject.clone({name:'jack'})
 * ...
 */
class JxObject {
  constructor() {}
  /**
   * @param {Object} {Array} sources拷贝的源对象,参数类型为对象或数组
   * @description 深拷贝数组或对象
   * @returns {Object} {Array} target拷贝之后的目标对象
   * @author xuemei.deng
   * @example
   * this.$jxUtils.vhObject.deepClone({name: 'jack', class: {grade: '六年级'}})
   */
  deepClone(sources) {
    if (!sources || sources !== 'object') throw new Error('参数不是对象或数组')
    const target = sources.constructor === Array ? [] : {}
    Object.keys(sources).forEach(key => {
      if (sources[key] && typeof sources[key] === 'object' ) {
        target[key] = this.deepClone(sources[key])
      } else {
        target[key] = sources[key]
      }
    })
    return target
  }

  /**
   *
   * @param {Object} {Array} sources拷贝的源对象，参数类型为对象或数组
   * @description 浅拷贝数组或对象
   * @returns {Object} {Array} target拷贝之后的目标对象
   * @author xuemei.deng
   * @example
   * this.$jxUtils.vhObject.clone({name: 'jack', age: 18}) // 返回对象 {nickname:'abc'}
   */
  clone(sources) {
    if (!sources || sources !== 'object') throw new Error('参数不是对象或数组')
    if (sources.constructor === Array) {
      return sources.slice()
    } else {
      return Object.assign({}, sources)
    }
  }
}

const vhObject = new JxObject()

export default vhObject

